
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.SubjectDao;
import com.sun.showcase.biz.mall.service.SubjectService;
import com.sun.showcase.client.domain.mall.Subject;
import com.sun.showcase.client.query.mall.SubjectQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("subjectService")
@Transactional
public class SubjectServiceImpl implements SubjectService{
	@Autowired
	private SubjectDao subjectDao;
	
	public void setSubjectDao(SubjectDao dao) {
		this.subjectDao = dao;
	}

	public DataGrid datagrid(SubjectQuery subjectQuery) {
		DataGrid j = new DataGrid();
		Pager<Subject> pager  = find(subjectQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<SubjectQuery> getQuerysFromEntitys(List<Subject> subjects) {
		List<SubjectQuery> subjectQuerys = new ArrayList<SubjectQuery>();
		if (subjects != null && subjects.size() > 0) {
			for (Subject tb : subjects) {
				SubjectQuery b = new SubjectQuery();
				BeanUtils.copyProperties(tb, b);
				subjectQuerys.add(b);
			}
		}
		return subjectQuerys;
	}

	private Pager<Subject> find(SubjectQuery subjectQuery) {
		Page<Subject> page = PageHelper.startPage(subjectQuery.getPage().intValue(), subjectQuery.getRows().intValue());
		List<Subject> list = subjectDao.findList(subjectQuery);
		Pager<Subject> pager = new Pager<Subject>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Subject add(SubjectQuery subjectQuery) {
		Subject t = new Subject();
		BeanUtils.copyProperties(subjectQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		subjectDao.save(t);
		subjectQuery.setId(t.getId());
		return t;
		
	}

	public void update(SubjectQuery subjectQuery) {
		Subject t = subjectDao.getById(subjectQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(subjectQuery, t);
	    	subjectDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(SubjectQuery subjectQuery) {
	    subjectDao.updatePart(subjectQuery);
	}

	public void delete(String[] ids) {
		subjectDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		subjectDao.deletePt(ids);
	}

	public Subject get(SubjectQuery subjectQuery) {
		return subjectDao.getById(subjectQuery.getId());
	}

	public Subject get(String id) {
		return subjectDao.getById(id);
	}

	
	public List<SubjectQuery> listAll(SubjectQuery subjectQuery) {
	    List<Subject> list = subjectDao.findList(subjectQuery);
		List<SubjectQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
